package com.turnengine.client.global.user.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.javabi.common.locale.LanguageCountry;
import com.turnengine.client.global.game.enums.GameLevel;
import com.turnengine.client.global.user.bean.IUser;
import com.turnengine.client.global.user.command.AdminVerifyUser;
import com.turnengine.client.global.user.command.CountUsers;
import com.turnengine.client.global.user.command.GetUser;
import com.turnengine.client.global.user.command.GetUserByEmail;
import com.turnengine.client.global.user.command.GetUserById;
import com.turnengine.client.global.user.command.GetUserByLoginId;
import com.turnengine.client.global.user.command.GetUserByName;
import com.turnengine.client.global.user.command.GetUserNames;
import com.turnengine.client.global.user.command.IAdminVerifyUser;
import com.turnengine.client.global.user.command.ICountUsers;
import com.turnengine.client.global.user.command.IGetUser;
import com.turnengine.client.global.user.command.IGetUserByEmail;
import com.turnengine.client.global.user.command.IGetUserById;
import com.turnengine.client.global.user.command.IGetUserByLoginId;
import com.turnengine.client.global.user.command.IGetUserByName;
import com.turnengine.client.global.user.command.IGetUserNames;
import com.turnengine.client.global.user.command.ILoginUser;
import com.turnengine.client.global.user.command.ILogoutUser;
import com.turnengine.client.global.user.command.IRenameUser;
import com.turnengine.client.global.user.command.ISetSignupVerification;
import com.turnengine.client.global.user.command.ISetUserAddress;
import com.turnengine.client.global.user.command.ISetUserGamePermission;
import com.turnengine.client.global.user.command.ISetUserLanguage;
import com.turnengine.client.global.user.command.ISetUserPermission;
import com.turnengine.client.global.user.command.ISignupUser;
import com.turnengine.client.global.user.command.IVerifyUser;
import com.turnengine.client.global.user.command.LoginUser;
import com.turnengine.client.global.user.command.LogoutUser;
import com.turnengine.client.global.user.command.RenameUser;
import com.turnengine.client.global.user.command.SetSignupVerification;
import com.turnengine.client.global.user.command.SetUserAddress;
import com.turnengine.client.global.user.command.SetUserGamePermission;
import com.turnengine.client.global.user.command.SetUserLanguage;
import com.turnengine.client.global.user.command.SetUserPermission;
import com.turnengine.client.global.user.command.SignupUser;
import com.turnengine.client.global.user.command.VerifyUser;
import com.turnengine.client.global.user.enums.UserPermissionType;
import java.util.List;

/**
 * The Global User Command Set.
 */
public class GlobalUserCommandSet implements IGlobalUserCommandSet {

	@Override
	public Boolean signupUser(String name, String password, String email, int address, LanguageCountry language) {
		ISignupUser command = new SignupUser(name, password, email, address, language);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean verifyUser(int id, long code, int address) {
		IVerifyUser command = new VerifyUser(id, code, address);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setUserAddress(int id, int address) {
		ISetUserAddress command = new SetUserAddress(id, address);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public long loginUser(String nameOrEmail, String password, int address) {
		ILoginUser command = new LoginUser(nameOrEmail, password, address);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Long> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean renameUser(long loginId, String from, String to) {
		IRenameUser command = new RenameUser(loginId, from, to);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean logoutUser(long loginId) {
		ILogoutUser command = new LogoutUser(loginId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setUserLanguage(long loginId, LanguageCountry language) {
		ISetUserLanguage command = new SetUserLanguage(loginId, language);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUser getUserById(long loginId, int id) {
		IGetUserById command = new GetUserById(loginId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUser> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUser getUserByName(long loginId, String name) {
		IGetUserByName command = new GetUserByName(loginId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUser> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUser getUserByEmail(long loginId, String email) {
		IGetUserByEmail command = new GetUserByEmail(loginId, email);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUser> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUser getUserByLoginId(long loginId, long id) {
		IGetUserByLoginId command = new GetUserByLoginId(loginId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUser> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public int countUsers() {
		ICountUsers command = new CountUsers();
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Integer> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean adminVerifyUser(long loginId, int id) {
		IAdminVerifyUser command = new AdminVerifyUser(loginId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setUserPermission(long loginId, int id, UserPermissionType type) {
		ISetUserPermission command = new SetUserPermission(loginId, id, type);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setUserGamePermission(long loginId, int userId, int id, UserPermissionType type, GameLevel level) {
		ISetUserGamePermission command = new SetUserGamePermission(loginId, userId, id, type, level);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setSignupVerification(long loginId, Boolean enabled) {
		ISetSignupVerification command = new SetSignupVerification(loginId, enabled);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUser getUser(long loginId) {
		IGetUser command = new GetUser(loginId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUser> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<String> getUserNames(long loginId) {
		IGetUserNames command = new GetUserNames(loginId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<String>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
